package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "我是目录")
public class EmployeeController {
    @Autowired
    HttpServletRequest httpResponse;

    @Autowired
    HttpServletRequest request;

    //   http://localhost:8080/admin/employee/login

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录功能")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO, HttpSession session) {
        Employee employee1 = new Employee();
        log.error("url: {}",request.getRequestURL());
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = null;

        employee = employeeService.login(employeeLoginDTO);

        // 用id做载荷 生成token并返回
        Long id = employee.getId();
        String token = JwtUtil.getToken(id);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        log.error("JET:{}",token);

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    //分页查询+名称模糊查询
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO page){
        log.error("page");

        //设置默认值  page:1 , PageSize:5
        if(page.getPage() == null){
            page.setPage(1);
        }
        if(page.getPageSize() == null){
            page.setPageSize(5);
        }

        PageResult page1 = employeeService.page(page);
        return Result.success(page1);
    }

    @PostMapping
    public Result addEmp(@RequestBody EmployeeDTO employeeDTO) throws Exception {
        employeeService.addEmp(employeeDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    public Result UpdateEmpStatus(@PathVariable Integer status,Integer id){
        Employee emp = Employee.builder().status(status)
                .id(Long.parseLong(id + "")).build();
        employeeService.UpDate(emp);

        return Result.success();
    }

    //
    @GetMapping("/{id}")
    public Result GetEmpById(@PathVariable Integer id) throws Exception {
        Employee emp = employeeService.getEmp(id);
        return Result.success(emp);
    }

    @PutMapping()
    public Result UpdateEmp(@RequestBody EmployeeDTO employeeDTO){
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeService.UpDate(employee);
        return Result.success();
    }



}
